/**
 * Title: jFamilyTree GXL provider
 * Description: jFamilyTree GXL provider
 * Version:	2.x
 * Copyright: (c) 2001-2008
 * Terms of use: see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 */
package jFamilyTree.provider.gxl;

import java.net.URL;

import jFamilyTree.model.Tree;
import jFamilyTree.model.IProvider;
import jFamilyTree.model.IProviderContext;
import jFamilyTree.model.Model;

/**
 * Provider for GXL
 * 
 * @author Bernard Bou
 */
public class Provider implements IProvider
{
	/**
	 * Provider context
	 */
	private IProviderContext theContext;

	/**
	 * Constructor
	 */
	public Provider()
	{
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jFamilyTree.model.IProvider#set(jFamilyTree.model.IProviderContext)
	 */
	public void connect(IProviderContext thisContext)
	{
		theContext = thisContext;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jFamilyTree.model.IProvider#makeModel(java.lang.String)
	 */
	public Model makeModel(String thisSource)
	{
		// get xml file
		String thisGxlFile = thisSource;
		if (thisGxlFile == null)
			thisGxlFile = theContext.getParameter("gxl");

		// parse XML file
		if (thisGxlFile != null)
		{
			// URL
			URL thisUrl = theContext.makeURL(thisGxlFile);
			if (thisUrl != null)
			{
				// parse
				theContext.putProgress("Loading ..." + thisGxlFile, false);
				Model thisModel = GxlParser.parseModel(thisUrl);
				if (thisModel != null)
				{
					theContext.putProgress("Loaded " + thisGxlFile, false);
					return thisModel;
				}
				theContext.putMessage("Cannot load GXL file <" + thisGxlFile + ">");
			}
			else
				theContext.putMessage("Bad URL " + thisGxlFile);
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jFamilyTree.model.IProvider#makeTree(java.lang.String, boolean)
	 */
	public Tree makeTree(String thisSource, boolean checkRecursion)
	{
		// get xml file
		String thisGxlFile = thisSource;
		if (thisGxlFile == null)
			thisGxlFile = theContext.getParameter("gxl");

		// parse XML file
		if (thisGxlFile != null)
		{
			// URL
			URL thisUrl = theContext.makeURL(thisGxlFile);
			if (thisUrl != null)
			{
				// parse
				theContext.putProgress("Loading ..." + thisGxlFile, false);
				Tree thisTree = GxlParser.parseTree(thisUrl);
				if (thisTree != null)
				{
					theContext.putProgress("Loaded " + thisGxlFile, false);
					return thisTree;
				}
				theContext.putMessage("Cannot load GXL file <" + thisGxlFile + ">");
			}
			else
				theContext.putMessage("Bad URL " + thisGxlFile);
		}
		return null;
	}
}
